﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HZ.DrugTrade.Model;
using HZ.DrugTrade.IBLL;
using HZ.DrugTrade.BLLFactory;
using HZ.DrugTrade.Model.Enum;

namespace HZ.DrugTrade.Web.Admin.DistributionManage
{
    public partial class DistributionRelationEdit : BasePage
    {

        IBLL_Distribution distCtrl = Factory.BLL_Distribution();
        IBLL_FlowEngine ife = Factory.BLL_FlowEngine();

        public DistributionRelationEdit()
        {
            this.ModuleNo = Module.配送关系管理.ToString();
            this.PageOperateType = OperateType.编辑.ToString();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            string procureCatalogId = Request.QueryString["procureCatalogId"];
            string hospitalId = Request.QueryString["hospitalId"];
            string companyId = Request.QueryString["companyId"];

            if (string.IsNullOrEmpty(procureCatalogId) || string.IsNullOrEmpty(hospitalId) || string.IsNullOrEmpty(companyId))
            {
                PageError("参数错误！", this.ReturnUrl);
            }

            if (!Page.IsPostBack)
            {
                IList<Flow> flowList = ife.GetFlows();
                dplFlow.DataSource = flowList;
                dplFlow.DataTextField = "FlowName";
                dplFlow.DataValueField = "FlowID";
                dplFlow.DataBind();

                HZ.DrugTrade.Model.Query.DistributionRelation aDistributionRelation = new HZ.DrugTrade.Model.Query.DistributionRelation();
                aDistributionRelation.ProcureCatalogID = procureCatalogId;
                aDistributionRelation.HospitalID = hospitalId;
                aDistributionRelation.CompanyID_PS = companyId;

                int totalCount = 0;
                IList<HZ.DrugTrade.Model.View.DistributionRelation> drList = distCtrl.GetDistributionByWhere(aDistributionRelation, 1, 0, out totalCount);
                if (drList.Count > 0)
                {
                    HZ.DrugTrade.Model.View.DistributionRelation dr = drList[0];
                    this.lblGoodsName.Text = dr.GoodsName;
                    this.lblHospitalName.Text = dr.HospitalName;
                    this.lblCompanyName.Text = dr.CompanyName_PS;
                    this.hdnPrice.Value = dr.ProcurePrice.ToString();
                    string isUsing = "";
                    if (dr.DistributionIsUsing.ToString() == false.ToString())
                    {
                        isUsing = "0";
                    }
                    else
                    {
                        isUsing = "1";
                    }
                    for (int i = 0; i < rbnUsing.Items.Count; i++)
                    {
                        if (rbnUsing.Items[i].Value == isUsing)
                        {
                            rbnUsing.Items[i].Selected = true;
                        }
                    }
                    for (int i = 0; i < dplFlow.Items.Count; i++)
                    {
                        if (dplFlow.Items[i].Value == dr.FlowID)
                        {
                            dplFlow.Items[i].Selected = true;
                        }
                    }
                }
                else
                {
                    PageError("配送关系不存在！", this.ReturnUrl);
                }

            }

        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            DistributionRelation distributionRelation = new DistributionRelation();
            distributionRelation.ProcureCatalogID = Request.QueryString["procureCatalogId"];
            distributionRelation.HospitalID = Request.QueryString["hospitalId"];
            distributionRelation.CompanyID_PS = Request.QueryString["companyId"];
            distributionRelation.FlowID = dplFlow.SelectedItem.Value.ToString();
            if (rbnUsing.SelectedItem.Value == "0")
            {
                distributionRelation.IsUsing = false;
            }
            else
            {
                distributionRelation.IsUsing = true;
            }
            distributionRelation.Price = new HZ.Common.ConvertHelper(this.hdnPrice.Value).ToDecimal;

            int flag=3;
            try
            {
                flag = distCtrl.UpdateDistribution(distributionRelation);
            }
            catch (Exception ex)
            {
                HZ.Web.WebUtil.Alert(ex.Message);
                return;
            }

            if (flag == 1)
            {
                PageInfo("保存成功！", this.ReturnUrl);
            }
            else
            {
                PageError("保存失败！", this.ReturnUrl);
            }
        }
    }
}
